package com.ruoyi.teaching.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;

import com.ruoyi.teaching.dto.YaTeachingPlanDTO;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.teaching.domain.YaTeachingPlan;
import com.ruoyi.teaching.service.IYaTeachingPlanService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 教学计划Controller
 *
 * @author ruoyi
 * @date 2025-09-16
 */
@RestController
@RequestMapping("/teaching_plan")
public class YaTeachingPlanController extends BaseController {
  @Autowired
  private IYaTeachingPlanService yaTeachingPlanService;

  /**
   * 查询教学计划列表
   */
  @RequiresPermissions("teaching:teaching_plan:list")
  @GetMapping("/list")
  public TableDataInfo list(YaTeachingPlan yaTeachingPlan) {
    startPage();
    List<YaTeachingPlan> list = yaTeachingPlanService.selectYaTeachingPlanList(yaTeachingPlan);
    return getDataTable(list);
  }


  @RequiresPermissions("teaching:teaching_plan:list")
  @GetMapping("/listall")
  public TableDataInfo listAll(YaTeachingPlanDTO yaTeachingPlan) {
    List<YaTeachingPlan> list = yaTeachingPlanService.selectYaTeachingPlanListAll(yaTeachingPlan);
    return getDataTable(list);
  }

  /**
   * 导出教学计划列表
   */
  @RequiresPermissions("teaching:teaching_plan:export")
  @Log(title = "教学计划", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, YaTeachingPlan yaTeachingPlan) {
    List<YaTeachingPlan> list = yaTeachingPlanService.selectYaTeachingPlanList(yaTeachingPlan);
    ExcelUtil<YaTeachingPlan> util = new ExcelUtil<YaTeachingPlan>(YaTeachingPlan.class);
    util.exportExcel(response, list, "教学计划数据");
  }

  /**
   * 获取教学计划详细信息
   */
  @RequiresPermissions("teaching:teaching_plan:query")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") Long id) {
    return success(yaTeachingPlanService.selectYaTeachingPlanById(id));
  }

  /**
   * 新增教学计划
   */
  @RequiresPermissions("teaching:teaching_plan:add")
  @Log(title = "教学计划", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@RequestBody YaTeachingPlan yaTeachingPlan) {
    return toAjax(yaTeachingPlanService.insertYaTeachingPlan(yaTeachingPlan));
  }

  @RequiresPermissions("teaching:teaching_plan:add")
  @Log(title = "教学计划创建预览", businessType = BusinessType.INSERT)
  @PostMapping("/preview")
  public AjaxResult preview(@RequestBody YaTeachingPlanDTO yaTeachingPlanDTO) {
    List<YaTeachingPlan> list = yaTeachingPlanService.previewTeachingPlan(yaTeachingPlanDTO);
    return AjaxResult.success(list);
  }

  /**
   * 添加工作日
   */
  @RequiresPermissions("teaching:teaching_plan:add")
  @PostMapping("/addWorkday")
  public TableDataInfo addWorkday(@RequestBody YaTeachingPlan yaTeachingPlan) {
    List<YaTeachingPlan> list = yaTeachingPlanService.addWorkday(yaTeachingPlan);
    return getDataTable(list);
  }

  /**
   * 添加节假日（休息日或自习日）
   */
  @RequiresPermissions("teaching:teaching_plan:add")
  @PostMapping("/addHoliday")
  public TableDataInfo addHoliday(@RequestBody YaTeachingPlan yaTeachingPlan) {
    List<YaTeachingPlan> list = yaTeachingPlanService.addHoliday(yaTeachingPlan);
    return getDataTable(list);
  }


  /**
   * 修改教学计划
   */
  @RequiresPermissions("teaching:teaching_plan:edit")
  @Log(title = "教学计划", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@RequestBody YaTeachingPlan yaTeachingPlan) {
    return toAjax(yaTeachingPlanService.updateYaTeachingPlan(yaTeachingPlan));
  }

  /**
   * 删除教学计划
   *
   * @param ids
   * @return
   */
  @RequiresPermissions("teaching:teaching_plan:remove")
  @DeleteMapping(value = "/{id}")
  public AjaxResult delete(@PathVariable("id") Long id) {
    return success(yaTeachingPlanService.deleteYaTeachingPlanById(id));
  }

  /**
   * 删除教学计划
   */
  @RequiresPermissions("teaching:teaching_plan:remove")
  @Log(title = "教学计划", businessType = BusinessType.DELETE)
  @DeleteMapping("/batch/{ids}")
  public AjaxResult remove(@PathVariable Long[] ids) {
    return toAjax(yaTeachingPlanService.deleteYaTeachingPlanByIds(ids));
  }


}
